package helper
{
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;

	import mx.events.FlexEvent;

	import view.NineTileLoadView;

	public class NinaTileLoadHelper extends NineTileLoadView
	{

		//リストの読み込みか、単体の読み込み可の判定
		private var listFlag:Boolean = false;
		//リストの読み込みか、単体の読み込み可の判定
		private var saveFlag:Boolean = false;

		//ボタンの名前を格納
		private var s:String;
		//何番目かの文字列を格納
		private var ss:String;

		//サウンドのファイルの場所を提議
		private var pathString:String;

		//扱うファイルを提議
		private var file:File;
		private var seURL:String;
		private var sound:Sound;
		private var channel:SoundChannel;

		private var countString:Array = new Array("one","two","three","four","five","six","seven","eight");


		public function NinaTileLoadHelper()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}


		//初期化
		public function init(event:FlexEvent):void{
			oneOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			twoOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			threeOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			fourOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			fiveOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			sixOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			sevenOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);
			eightOpen.addEventListener(MouseEvent.CLICK,fileOpenHandler);

			onePlay.addEventListener(MouseEvent.CLICK,soundPlay);
			twoPlay.addEventListener(MouseEvent.CLICK,soundPlay);
			threePlay.addEventListener(MouseEvent.CLICK,soundPlay);
			fourPlay.addEventListener(MouseEvent.CLICK,soundPlay);
			fivePlay.addEventListener(MouseEvent.CLICK,soundPlay);
			sixPlay.addEventListener(MouseEvent.CLICK,soundPlay);
			sevenPlay.addEventListener(MouseEvent.CLICK,soundPlay);
			eightPlay.addEventListener(MouseEvent.CLICK,soundPlay);

//			oneStop.addEventListener(MouseEvent.CLICK,soundStop);
//			twoStop.addEventListener(MouseEvent.CLICK,soundStop);
//			threeStop.addEventListener(MouseEvent.CLICK,soundStop);

			saveButton.addEventListener(MouseEvent.CLICK,soundListSaveHandler);
			loadButton.addEventListener(MouseEvent.CLICK,soundListLoadHandler);

			closeButton.addEventListener(MouseEvent.CLICK, appClose);

			file = new File();
			file.addEventListener(Event.SELECT,seSelectHandler);
		}

		//fileOpenボタンのハンドリング
		public function fileOpenHandler(event:MouseEvent):void{
			//ボタンの名前からどこのボタンを触ったか格納する
			s = event.target.toString();
			ss = s.slice(s.lastIndexOf(".")+1,s.length);
			trace(ss.slice(0,ss.lastIndexOf("Open")) + "Path");
			trace(this);

			listFlag = true;
			file.browse();


			/* trace(event.currentTarget.name)
			trace(getChildByName(event.currentTarget.name.toString()));

			var btnName:String = event.currentTarget.name.toString();

			Label(getChildByName(btnName.slice(0,btnName.lastIndexOf("Open")) + "Path")).text = btnName; */
		}

		private var csvline:String;
		private var n:Number;

		//ファイルオブジェクトの操作全部
		public function seSelectHandler(event:Event):void{
			if(listFlag){
				seURL = file.nativePath;
				trace(file.nativePath);
				this[ss.slice(0,ss.lastIndexOf("Open")) + "Path"].text = seURL;
//				this[ss.slice(0,ss.lastIndexOf("Open")) + "Name"].text = file.name;
			}else{
				//ここからCSV入力・出力ロジック
				var select:File = event.target as File;
				if(saveFlag){
					var fsw:FileStream = new FileStream();
					fsw.open(select, FileMode.WRITE);
					fsw.writeUTFBytes(onePath.text.replace("\\","/")+","+twoPath.text.replace("\\","/")+","+threePath.text.replace("\\","/")+","+fourPath.text.replace("\\","/") + ","
						+ fivePath.text.replace("\\","/")+","+sixPath.text.replace("\\","/")+","+sevenPath.text.replace("\\","/")+","+eightPath.text.replace("\\","/") + ",");
					fsw.close();
					trace("保存");
				}else{
					n = 0;
					trace("開いた！！" + file.nativePath);
					var fs:FileStream = new FileStream();
					fs.open(select,FileMode.READ);
					csvline = fs.readMultiByte(file.size,"utf-8");
					for(var i:int=0;i < countString.length;i++){
						trace(this[countString[i]+"Path"].text);
						if(i == 0){
							trace(csvline.length)
							this[countString[i]+"Path"].text = csvline.slice(i,csvline.indexOf(","));
//							this[countString[i]+"Name"].text = this[countString[i]+"Path"].text.slice(this[countString[i]+"Path"].text.lastIndexOf("/")+1,this[countString[i]+"Path"].text.length);
						}else{

							trace("!!!!" + csvline,this[countString[i-1]+"Path"].text,csvline.length);

							n = n + this[countString[i-1]+"Path"].text.length

							this[countString[i]+"Path"].text =
								csvline.slice(csvline.indexOf(",",n)+1,csvline.indexOf(".mp3",n)+4);
//							this[countString[i]+"Name"].text = this[countString[i]+"Path"].text.slice(this[countString[i]+"Path"].text.lastIndexOf("/")+1,this[countString[i]+"Path"].text.length);

						}
						trace(i,countString[i]);
					}

					fs.close();
				}
			}

		//	sound.addEventListener(Event.COMPLETE,seLoadHandler);
		}

		public function seLoadHandler(event:Event):void{
			this[ss.slice(0,ss.lastIndexOf("Open")) + "Path"].text = seURL;
		}

		public function soundPlay(event:MouseEvent):void{
			sound = new Sound();
			channel = new SoundChannel();
			sound.load(new URLRequest(this[event.currentTarget.name.slice(0,event.currentTarget.name.lastIndexOf("Play")) + "Path"].text));
//			soundPathText.text = "playButtonClick";
			try{
				channel = sound.play()
			}catch(e:Error){

			}
		}


		public function soundListSaveHandler(event:MouseEvent):void{
			listFlag = false;
			saveFlag = true;
			file.browseForSave("リストの保存");
		}

		public function soundListLoadHandler(event:MouseEvent):void{
			listFlag = false;
			saveFlag = false;
			file.browseForOpen("リストの読み込み");

		}

		public function soundStop(event:MouseEvent):void{
			channel.stop();
		}

		public function appClose(event:MouseEvent):void{

		}
	}
}